/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DbSelectionInterface.java
 *
 * Created on Oct 20, 2009, 8:32:16 PM
 */
package give.replay.gui;

import give.replay.config.ReplayConfiguration;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author koller
 */
public class DbSelectionInterface extends javax.swing.JFrame {

    private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private AbstractMain main;
//    private ReplayConfiguration replayConfig;

    /** Creates new form DbSelectionInterface */
    public DbSelectionInterface(AbstractMain main, List<Map<String, Object>> entries, ReplayConfiguration replayConfig) {
        setLocationByPlatform(true);

        this.main = main;
        initComponents();

        gamesTable.setModel(replayConfig.getTableModel());
        initGamesTable(entries, replayConfig);

        getRootPane().setDefaultButton(okButton);
    }

    private void initGamesTable(List<Map<String, Object>> entries, ReplayConfiguration replayConfig) {
        gamesTable.setAutoCreateRowSorter(true);

        DefaultTableModel model = (DefaultTableModel) gamesTable.getModel();
        model.setRowCount(entries.size());
        int row = 0;

        for (Map<String, Object> entriesHere : entries) {
            int column = 0;

            for (String dbLabel : replayConfig.getDbColumns()) {
                Object x = entriesHere.get(dbLabel);

                if (x == null) {
                    model.setValueAt("null", row, column);
                } else if (x instanceof Date) {
                    model.setValueAt(format.format((Date) x), row, column);
                } else {
                    model.setValueAt(x, row, column);
                }

                column++;
            }

            row++;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        gamesTable = new javax.swing.JTable();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Select game from database");

        gamesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Subject ID", "Start time", "End time", "World", "Language"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        gamesTable.setGridColor(new java.awt.Color(0, 0, 0));
        gamesTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        gamesTable.setShowHorizontalLines(false);
        gamesTable.setShowVerticalLines(false);
        gamesTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                gamesTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(gamesTable);

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(okButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(okButton)
                    .add(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // CAUTION: don't use evt -- this method is called with a null argument
        int row = gamesTable.convertRowIndexToModel(gamesTable.getSelectedRow());
        int gameid = (Integer) gamesTable.getModel().getValueAt(row, 0);

        setVisible(false);

        if (main != null) {
            main.openMap(gameid);
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void gamesTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gamesTableMouseClicked
        if (evt.getClickCount() == 2) {
            okButtonActionPerformed(null);
        }
    }//GEN-LAST:event_gamesTableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTable gamesTable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
}
